package model;

import java.util.ArrayList;
import java.util.List;
import org.bson.types.ObjectId;
import com.google.code.morphia.dao.BasicDAO;
import com.google.code.morphia.query.Query;
import entity.Question;

public class QuestionModel extends BasicDAO<Question, ObjectId> {

	private int pageSize = 15;

	public QuestionModel() {
		super(DBConnection.getMongo(), DBConnection.getMorphia(), DBConnection
				.getDatabase());
		// ds.ensureIndexes();
	}

	public List<Question> all(String lang, int currentPage) {
		Query<Question> q = ds.createQuery(Question.class);
		q.field("hadContent").equals(1);
		q.field("lang").equal(lang);
		q.order("-lastUpdateDate");
		return q.limit(pageSize).offset(currentPage * pageSize).asList();
	}

	public List<Question> findByAuthor(String authorAlias, String lang,
			int currentPage) {
		Query<Question> q = ds.createQuery(Question.class);
		q.field("hadContent").equals(1);
		q.field("lang").equal(lang);
		q.field("aliasAuthor").equal(authorAlias);
		return q.limit(pageSize).offset(currentPage * pageSize).asList();
	}

	public List<Question> findByAnswer(String answerAlias, String lang,
			int currentPage) {
		Query<Question> q = ds.createQuery(Question.class);
		q.field("hadContent").equals(1);
		q.field("lang").equal(lang);
		q.field("aliasAuthorAnswer").equal(answerAlias);
		return q.limit(pageSize).offset(currentPage * pageSize).asList();
	}

	public List<Question> findByTag(String tagAlias, String lang,
			int currentPage) {
		Query<Question> q = ds.createQuery(Question.class);
		q.field("hadContent").equals(1);
		q.field("lang").equal(lang);
		q.field("tags").equal(tagAlias);
		return q.limit(pageSize).offset(currentPage * pageSize).asList();
	}

	public Question findOneByTag(String tagAlias, String lang) {
		Query<Question> q = ds.find(Question.class);
		q.field("hadContent").equals(1);
		q.field("lang").equal(lang);
		q.field("tags").equal(tagAlias);
		q.order("-doc");
		return q.get();
	}

	public List<Question> getBrotherQuestion(ArrayList<String> tags, String lang) {
		List<Question> list = new ArrayList<Question>();
		Question question = new Question();
		for (String tag : tags) {
			question = findOneByTag(tag, lang);
			if (question != null) {
				if (list.size() > 0) {
					boolean had = false;
					for (int i = 0; i < list.size(); i++) {
						if (list.get(i).getAlias().equals(question.getAlias())) {
							had = true;
						}
					}
					if (!had) {
						list.add(question);
					}
				} else {
					list.add(question);
				}
			}
		}
		if (list.size() > 8) {
			list = list.subList(0, 7);
		}
		return list;
	}

	public synchronized Question getQuestionNoContent() {
		Query<Question> q = ds.find(Question.class);
		q.field("hadContent").equal(0);
		q.field("lang").equal(null);
		Question obj = q.get();
		if (obj == null || obj.getUrl() == null) {
			return null;
		}
		obj.setHadContent(1);
		ds.save(obj);
		return obj;
	}

	public Question findByAlias(String alias, String lang) {
		Query<Question> q = ds.find(Question.class);
		q.field("hadContent").equals(1);
		q.field("lang").equal(lang);
		q.field("alias").equal(alias);
		return q.get();
	}

	public Question findByUrl(String url) {
		Query<Question> q = ds.find(Question.class);
		q.field("url").equals(url);
		return q.get();
	}

	public List<Question> getPrevious(Question current, String lang) {
		Query<Question> q = ds.createQuery(Question.class);
		q.field("lang").equal(lang);
		q.field("hadContent").equal(1);
		q.field("doc").lessThan(current.getDoc());
		q.order("-doc");
		return q.limit(8).asList();
	}

	public boolean isGotten(String url) {
		Query<Question> q = ds.find(Question.class);
		q.field("url").equal(url);
		return q.get() == null ? false : true;
	}

	public static void main(String[] args) {
		System.out.println("start");
		Question q = new Question();
		q.setUrl("aajsahsjah");
		QuestionModel model = new QuestionModel();
		System.out.println("Alo" + model.isGotten(q.getUrl()));
	}
}
